package com.example.weather.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.weather.Base.BaseActivity;
import com.example.weather.Bean.Weatherbean;
import com.example.weather.R;
import com.google.gson.Gson;

public class AddcityActivity extends BaseActivity implements View.OnClickListener {
    private EditText search_et;
    private ImageView search_iv_submit;
    private GridView search_gv;
    private  String[] hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州",
            "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"};
    private ArrayAdapter<String> adapter;
    String url1 = "http://api.map.baidu.com/telematics/v3/weather?location=";
    String url2 = "&output=json&ak=FkPhtMBK0HTIQNh7gG4cNUttSTyr0nzo";
    private String city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_city);
        search_et=findViewById(R.id.search_et);
        search_iv_submit=findViewById(R.id.search_iv_submit);
        search_gv=findViewById(R.id.search_gv);
        adapter = new ArrayAdapter<>(this, R.layout.item_hotcity, hotCitys);
        search_gv.setAdapter(adapter);
        search_iv_submit.setOnClickListener(this);
        setListener();
    }

    //grideview监听事件
    private void setListener() {
        search_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                city=hotCitys[position];
                String url=url1+city+url2;
                loaddata(url);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_iv_submit:
                city = search_et.getText().toString();
                if (!TextUtils.isEmpty(city)) {
                    //判断城市是否正确
                    String url=url1+city+url2;
                    loaddata(url);
                }else {
                    Toast.makeText(this,"输入内容不能为空",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onSuccess(String result) {
        Weatherbean weatherbean = new Gson().fromJson(result, Weatherbean.class);
        if (weatherbean.getError()==0) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("city",city);
            startActivity(intent);
        }else {
            Toast.makeText(this,"暂时无此城市的天气信息",Toast.LENGTH_SHORT).show();
        }
    }
}
